/*
 * PersonActions.java
 */

package nxtgeninventorysystem.Controller;

import java.util.ArrayList;
import nxtgeninventorysystem.Data.Person;
import nxtgeninventorysystem.Data.PersonSearchOptions;
import nxtgeninventorysystem.Model.Database;
import nxtgeninventorysystem.View.Dialog.CreateTeam;
import nxtgeninventorysystem.View.Dialog.EditTeam;
import nxtgeninventorysystem.View.Dialog.ModifyPerson;
import nxtgeninventorysystem.View.Gui;
import nxtgeninventorysystem.View.Gui.Dialog;

/**
 * The PersonActions class is a Controller class inside the Model-View-Controller
 * design pattern. This class typically deals with the Person entity in the
 * database, allowing flow of data between the View (Gui) and the Model
 * (Database).
 * @author Seraph Softworks
 */
public final class PersonActions extends Actions {

    public PersonActions() {

    }

    public void gotoCreatePerson() {
        Thread t = new Thread() {
            public void run() {
                while(busy) {

                }
                busy = true;

                ModifyPerson form = (ModifyPerson) Gui.getDialog(Dialog.CreatePerson);
                form.setTargetPerson(null);
                form.resetFields();

                busy = false;

                Gui.showDialog(Dialog.CreatePerson);
            }
        };
        t.start();
    }

    public void doCreatePerson() {
        Thread t = new Thread() {
            public void run() {
                ModifyPerson form = (ModifyPerson) Gui.getDialog(Dialog.CreatePerson);
                form.setEnabled(false);
                while(busy) {

                }
                busy = true;

                Gui.setBusyEffect(true);
                Gui.setStatusBarMessage("Creating New Person...");

                Person person = form.getFormPerson();
                int id = Database.person.createPerson(person);

                Gui.setReadPersonPage(Database.person.readPerson(id),Database.person.getPersonItems(id,Gui.getItemOrderAtPage(Gui.Page.ReadPerson),true),Database.person.getPersonTransactions(id,Gui.getTransactionOrderAtPage(Gui.Page.ReadPerson),true));

                form.setEnabled(true);
                form.dispose();
                Gui.setBusyEffect(false);
                Gui.setStatusBarMessage("Ready...");

                busy = false;

                Actions.person.gotoBrowsePerson();
            }
        };
        t.start();
    }

    public void gotoBrowsePerson() {
        Thread t = new Thread() {
            public void run() {
                while(busy) {

                }
                busy = true;

                Gui.goToPage(Gui.Page.BrowsePerson);

                busy = false;

                Actions.person.doSearchPerson();
            }
        };
        t.start();
    }

    public void doSearchPerson() {
        Thread t = new Thread() {
            public void run() {
                Gui.setEnabled(false);
                while(busy) {

                }
                busy = true;

                Gui.setBusyEffect(true);
                Gui.setStatusBarMessage("Retreiving Search Results...");

                Gui.setBrowsePersonPage(new ArrayList <Person> ());

                Person values = Gui.getPersonSearchValues(Gui.getCurrentPage());
                PersonSearchOptions options = Gui.getPersonSearchOptions(Gui.getCurrentPage());
                ArrayList <Person> personlist = null;
                PersonSearchOptions order = Gui.getPersonOrderAtPage(Gui.getCurrentPage());

                switch(Gui.getCurrentPage()) {
                    case BrowsePerson:
                        personlist = Database.person.searchPerson(values, options, order, true);
                        Gui.setBrowsePersonPage(personlist);
                        break;
                    case BorrowItem:
                        personlist = Database.person.searchPerson(values, options, order, true);
                        Gui.setBorrowItems(personlist, Gui.getDisplayedItemsAtPage(Gui.Page.BorrowItem));
                        break;
                }

                Gui.setBusyEffect(false);
                Gui.setStatusBarMessage("Ready...");

                Gui.setEnabled(true);

                busy = false;
            }
        };
        t.start();
    }

    public void gotoReadPerson() {
        Thread t = new Thread() {
            public void run() {
                Gui.setEnabled(false);
                while(busy) {

                }
                busy = true;

                Gui.setBusyEffect(true);
                Gui.setStatusBarMessage("Retreiving Person Information...");

                Person person = null;

                switch(Gui.getCurrentPage()) {
                    case ReadPerson:
                        person = Gui.getReadPersonTarget();
                        break;
                    case ReadTransaction:
                        person = Gui.getReadTransactionTarget().getPerson();
                        break;
                    default:
                        ArrayList <Person> persons = Gui.getPersonsSelectedAtPage(Gui.getCurrentPage());
                        if(persons == null)
                            return;
                        person = persons.get(0);
                        break;
                }

                if(person == null)
                    return;

                Gui.setReadPersonPage(Database.person.readPerson(person.getId()),Database.person.getPersonItems(person.getId(),Gui.getItemOrderAtPage(Gui.Page.ReadPerson),true),Database.person.getPersonTransactions(person.getId(),Gui.getTransactionOrderAtPage(Gui.Page.ReadPerson),true));

                Gui.setBusyEffect(false);
                Gui.setStatusBarMessage("Ready...");

                Gui.setEnabled(true);

                Gui.goToPage(Gui.Page.ReadPerson);

                busy = false;
            }
        };
        t.start();
    }

    public void gotoUpdatePerson() {
        Thread t = new Thread() {
            public void run() {
                while(busy) {

                }
                busy = true;

                Person person = Gui.getReadPersonTarget();

                if(person == null)
                    return;

                ModifyPerson form = (ModifyPerson) Gui.getDialog(Dialog.EditPerson);
                form.setTargetPerson(person);
                form.resetFields();

                busy = false;

                Gui.showDialog(Dialog.EditPerson);
            }
        };
        t.start();
    }

    public void doUpdatePerson() {
        Thread t = new Thread() {
            public void run() {
                ModifyPerson form = (ModifyPerson) Gui.getDialog(Dialog.EditPerson);
                form.setEnabled(false);
                while(busy) {

                }
                busy = true;

                Gui.setBusyEffect(true);
                Gui.setStatusBarMessage("Updating Person Information...");

                Person formperson = form.getFormPerson();
                Person primeperson = form.getTargetPerson();
                primeperson.setName(formperson.getName());
                primeperson.setTeam(formperson.getTeam());
                primeperson.setContactNo(formperson.getContactNo());

                Database.person.updatePerson(primeperson);

                Gui.setReadPersonPage(Database.person.readPerson(primeperson.getId()),Database.person.getPersonItems(primeperson.getId(),Gui.getItemOrderAtPage(Gui.Page.ReadPerson),true),Database.person.getPersonTransactions(primeperson.getId(),Gui.getTransactionOrderAtPage(Gui.Page.ReadPerson),true));

                form.setEnabled(true);
                form.dispose();
                Gui.setBusyEffect(false);
                Gui.setStatusBarMessage("Ready...");

                busy = false;

                Gui.goToPage(Gui.Page.ReadPerson);
            }
        };
        t.start();
    }

    public void gotoCreateTeam() {
        Thread t = new Thread() {
            public void run() {
                while(busy) {

                }
                busy = true;

                CreateTeam form = (CreateTeam) Gui.getDialog(Dialog.CreateTeam);
                form.resetFields();

                busy = false;

                Gui.showDialog(Dialog.CreateTeam);
            }
        };
        t.start();
    }

    public void doCreateTeam() {
        Thread t = new Thread() {
            public void run() {
                CreateTeam form = (CreateTeam) Gui.getDialog(Dialog.CreateTeam);
                ModifyPerson crt = (ModifyPerson) Gui.getDialog(Dialog.CreatePerson);
                ModifyPerson edi = (ModifyPerson) Gui.getDialog(Dialog.EditPerson);
                form.setEnabled(false);
                crt.setEnabled(false);
                edi.setEnabled(false);
                while(busy) {

                }
                busy = true;

                Gui.setBusyEffect(true);
                Gui.setStatusBarMessage("Creating New Team...");

                String team = form.getTeamName();
                Database.person.createTeam(team);

                Gui.refreshComboBoxes(Database.item.readItemTypes(), Database.person.readTeams());

                form.setEnabled(true);
                form.dispose();

                Gui.setBusyEffect(false);
                Gui.setStatusBarMessage("Ready...");

                busy = false;

                crt.setEnabled(true);
                edi.setEnabled(true);

                Actions.person.gotoBrowsePerson();
            }
        };
        t.start();
    }

    public void gotoUpdateTeam() {
        Thread t = new Thread() {
            public void run() {
                while(busy) {

                }
                busy = true;

                EditTeam form = (EditTeam) Gui.getDialog(Dialog.EditTeam);
                form.resetFields();

                busy = false;

                Gui.showDialog(Dialog.EditTeam);
            }
        };
        t.start();
    }

    public void doUpdateTeam() {
        Thread t = new Thread() {
            public void run() {
                EditTeam form = (EditTeam) Gui.getDialog(Dialog.EditTeam);
                form.setEnabled(false);
                while(busy) {

                }
                busy = true;

                Gui.setBusyEffect(true);
                Gui.setStatusBarMessage("Updating Team Information...");

                String oldteam = form.getOldTeam();
                String newteam = form.getNewTeam();
                Database.person.updateTeam(oldteam, newteam);

                Gui.refreshComboBoxes(Database.item.readItemTypes(), Database.person.readTeams());

                form.setEnabled(true);
                form.dispose();

                Gui.goToPage(Gui.Page.BrowsePerson);
                Gui.setBusyEffect(false);
                Gui.setStatusBarMessage("Ready...");

                busy = false;

                Actions.person.gotoBrowsePerson();
            }
        };
        t.start();
    }

    public void doRefreshTeam() {
        Thread t = new Thread() {
            public void run() {
                Gui.setEnabled(false);
                while(busy) {

                }
                busy = true;

                Gui.setBusyEffect(true);
                Gui.setStatusBarMessage("Refreshing Item Types...");

                Gui.refreshComboBoxes(Database.item.readItemTypes(), Database.person.readTeams());

                Gui.setBusyEffect(false);
                Gui.setStatusBarMessage("Ready...");

                Gui.setEnabled(true);

                busy = false;

                Actions.person.gotoBrowsePerson();
            }
        };
        t.start();
    }

}
